package com.yanftch.stopcoding.utils;

import com.yanftch.stopcoding.bean.HotBean;

import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class FileUtils {
    private static final String path = "files/default_text.json";

    public static final List<HotBean> getList() {
        List<HotBean> list = parseJsonToList(readJsonFile(path));
        return list;
//        List<HotBean> resultList = new ArrayList<>();
//        for (HotBean hotBean : list) {
//            String[] titles = hotBean.getTitle().split("，"); // 使用逗号拆分
//            for (String title : titles) {
//                resultList.add(new HotBean(title.trim())); // 添加到新的 List 并去除多余空格
//            }
//        }
//        return resultList;
    }

    private static List<HotBean> parseJsonToList(String jsonContent) {
        return GsonUtils.INSTANCE.jsonToList(jsonContent, HotBean.class);
    }

    private static String readJsonFile(String filePath) {
        InputStream inputStream = FileUtils.class.getClassLoader().getResourceAsStream(filePath);
        if (inputStream == null) {
            throw new RuntimeException("File not found: " + filePath);
        }

        try (Scanner scanner = new Scanner(inputStream, StandardCharsets.UTF_8.name())) {
            return scanner.useDelimiter("\\A").next();  // 读取整个文件为字符串
        }
    }
}
